package com.ruoyi.zsyh.controller;

import java.util.List;

import com.ruoyi.common.utils.IdWorker;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.zsyh.domain.ZsyhAfterProductMoney;
import com.ruoyi.zsyh.service.IZsyhAfterProductMoneyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 厂家产品下单时费用Controller
 * 
 * @author yuexl
 * @date 2022-08-22
 */
@Controller
@RequestMapping("/zsyh/afterProductMoney")
public class ZsyhAfterProductMoneyController extends BaseController
{
    private String prefix = "zsyh/afterProductMoney";

    @Autowired
    private IZsyhAfterProductMoneyService zsyhAfterProductMoneyService;

    @RequiresPermissions("zsyh:afterProductMoney:view")
    @GetMapping()
    public String afterProductMoney()
    {
        return prefix + "/afterProductMoney";
    }

    /**
     * 查询厂家产品下单时费用列表
     */
    @RequiresPermissions("zsyh:afterProductMoney:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ZsyhAfterProductMoney zsyhAfterProductMoney)
    {
        startPage();
        List<ZsyhAfterProductMoney> list = zsyhAfterProductMoneyService.selectZsyhAfterProductMoneyList(zsyhAfterProductMoney);
        return getDataTable(list);
    }

    /**
     * 导出厂家产品下单时费用列表
     */
    @RequiresPermissions("zsyh:afterProductMoney:export")
    @Log(title = "厂家产品下单时费用", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ZsyhAfterProductMoney zsyhAfterProductMoney)
    {
        List<ZsyhAfterProductMoney> list = zsyhAfterProductMoneyService.selectZsyhAfterProductMoneyList(zsyhAfterProductMoney);
        ExcelUtil<ZsyhAfterProductMoney> util = new ExcelUtil<ZsyhAfterProductMoney>(ZsyhAfterProductMoney.class);
        return util.exportExcel(list, "厂家产品下单时费用数据");
    }

    /**
     * 新增厂家产品下单时费用
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存厂家产品下单时费用
     */
    @RequiresPermissions("zsyh:afterProductMoney:add")
    @Log(title = "厂家产品下单时费用", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ZsyhAfterProductMoney zsyhAfterProductMoney)
    {
        zsyhAfterProductMoney.setId(IdWorker.getId());
        //先判断该产品该服务分类是否已经存在，已经存在则不能新增
        ZsyhAfterProductMoney zsyhAfterProductMoney1 = new ZsyhAfterProductMoney();
        zsyhAfterProductMoney1.setServiceTypeId(zsyhAfterProductMoney.getServiceTypeId());
        zsyhAfterProductMoney1.setProductId(zsyhAfterProductMoney.getProductId());
        if(zsyhAfterProductMoneyService.selectZsyhAfterProductMoneyList(zsyhAfterProductMoney1).size()>0){
            return error("该产品同种服务分类已经存在，不能重复添加");
        }
        return toAjax(zsyhAfterProductMoneyService.insertZsyhAfterProductMoney(zsyhAfterProductMoney));
    }

    /**
     * 修改厂家产品下单时费用
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        ZsyhAfterProductMoney zsyhAfterProductMoney = zsyhAfterProductMoneyService.selectZsyhAfterProductMoneyById(id);
        mmap.put("zsyhAfterProductMoney", zsyhAfterProductMoney);
        return prefix + "/edit";
    }

    /**
     * 修改保存厂家产品下单时费用
     */
    @RequiresPermissions("zsyh:afterProductMoney:edit")
    @Log(title = "厂家产品下单时费用", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ZsyhAfterProductMoney zsyhAfterProductMoney)
    {
        //先判断该产品该服务分类是否已经存在，已经存在则不能新增
        ZsyhAfterProductMoney zsyhAfterProductMoney1 = new ZsyhAfterProductMoney();
        zsyhAfterProductMoney1.setServiceTypeId(zsyhAfterProductMoney.getServiceTypeId());
        zsyhAfterProductMoney1.setProductId(zsyhAfterProductMoney.getProductId());
        List<ZsyhAfterProductMoney> zsyhAfterProductMoneyList = zsyhAfterProductMoneyService.selectZsyhAfterProductMoneyList(zsyhAfterProductMoney1);
        if(zsyhAfterProductMoneyList.size()>0){
            if(!zsyhAfterProductMoneyList.get(0).getId().equals(zsyhAfterProductMoney.getId())){
                return error("该产品同种服务分类已经存在，不能重复添加");
            }
        }
        return toAjax(zsyhAfterProductMoneyService.updateZsyhAfterProductMoney(zsyhAfterProductMoney));
    }

    /**
     * 删除厂家产品下单时费用
     */
    @RequiresPermissions("zsyh:afterProductMoney:remove")
    @Log(title = "厂家产品下单时费用", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(zsyhAfterProductMoneyService.deleteZsyhAfterProductMoneyByIds(ids));
    }
}
